<!DOCTYPE html>
<html lang="zh-CN">
<head>
  <meta charset="utf-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
  <title> - 服务计算 - 中山大学</title>
  <meta name="renderer" content="webkit" />
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1"/>

<meta http-equiv="Cache-Control" content="no-transform" />
<meta http-equiv="Cache-Control" content="no-siteapp" />

<meta name="theme-color" content="#f8f5ec" />
<meta name="msapplication-navbutton-color" content="#f8f5ec">
<meta name="apple-mobile-web-app-capable" content="yes">
<meta name="apple-mobile-web-app-status-bar-style" content="#f8f5ec">


<meta name="author" content="潘茂林" /><meta name="description" content="CLI 命令行实用程序开发基础 1、概述 CLI（Command Line Interface）实用程序是Linux下应用开发的基础。正确的编写命令行程序让应用" /><meta name="keywords" content="中山大学, 数据科学与计算机, 服务计算" />






<meta name="generator" content="Hugo 0.74.3 with theme even" />


<link rel="canonical" href="http://pmlpml.git.io/service-computing/post/ex-cli-basic/" />
<link rel="apple-touch-icon" sizes="180x180" href="/service-computing/apple-touch-icon.png">
<link rel="icon" type="image/png" sizes="32x32" href="/service-computing/favicon-32x32.png">
<link rel="icon" type="image/png" sizes="16x16" href="/service-computing/favicon-16x16.png">
<link rel="manifest" href="/service-computing/manifest.json">
<link rel="mask-icon" href="/service-computing/safari-pinned-tab.svg" color="#5bbad5">



<link href="/service-computing/sass/main.min.8c3cbcb0324c2bb4875ceccba4007cbad4b4ac8377f33af9953c3e7684534a50.css" rel="stylesheet">
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@fancyapps/fancybox@3.1.20/dist/jquery.fancybox.min.css" integrity="sha256-7TyXnr2YU040zfSP+rEcz29ggW4j56/ujTPwjMzyqFY=" crossorigin="anonymous">


<meta property="og:title" content="" />
<meta property="og:description" content="CLI 命令行实用程序开发基础 1、概述 CLI（Command Line Interface）实用程序是Linux下应用开发的基础。正确的编写命令行程序让应用" />
<meta property="og:type" content="article" />
<meta property="og:url" content="http://pmlpml.git.io/service-computing/post/ex-cli-basic/" />

<meta itemprop="name" content="">
<meta itemprop="description" content="CLI 命令行实用程序开发基础 1、概述 CLI（Command Line Interface）实用程序是Linux下应用开发的基础。正确的编写命令行程序让应用">

<meta itemprop="wordCount" content="893">



<meta itemprop="keywords" content="" />
<meta name="twitter:card" content="summary"/>
<meta name="twitter:title" content=""/>
<meta name="twitter:description" content="CLI 命令行实用程序开发基础 1、概述 CLI（Command Line Interface）实用程序是Linux下应用开发的基础。正确的编写命令行程序让应用"/>

<!--[if lte IE 9]>
  <script src="https://cdnjs.cloudflare.com/ajax/libs/classlist/1.1.20170427/classList.min.js"></script>
<![endif]-->

<!--[if lt IE 9]>
  <script src="https://cdn.jsdelivr.net/npm/html5shiv@3.7.3/dist/html5shiv.min.js"></script>
  <script src="https://cdn.jsdelivr.net/npm/respond.js@1.4.2/dest/respond.min.js"></script>
<![endif]-->

</head>
<body>
  <div id="mobile-navbar" class="mobile-navbar">
  <div class="mobile-header-logo">
    <a href="/service-computing/" class="logo">服务计算</a>
  </div>
  <div class="mobile-navbar-icon">
    <span></span>
    <span></span>
    <span></span>
  </div>
</div>
<nav id="mobile-menu" class="mobile-menu slideout-menu">
  <ul class="mobile-menu-list">
    <a href="/service-computing/post/index-2020">
        <li class="mobile-menu-item">首页</li>
      </a><a href="/service-computing/post/">
        <li class="mobile-menu-item">归档</li>
      </a><a href="/service-computing/tags/">
        <li class="mobile-menu-item">标签</li>
      </a><a href="/service-computing/about/">
        <li class="mobile-menu-item">关于</li>
      </a>
  </ul>
</nav>
  <div class="container" id="mobile-panel">
    <header id="header" class="header">
        <div class="logo-wrapper">
  <a href="/service-computing/" class="logo">服务计算</a>
</div>

<nav class="site-navbar">
  <ul id="menu" class="menu">
    <li class="menu-item">
        <a class="menu-item-link" href="/service-computing/post/index-2020">首页</a>
      </li><li class="menu-item">
        <a class="menu-item-link" href="/service-computing/post/">归档</a>
      </li><li class="menu-item">
        <a class="menu-item-link" href="/service-computing/tags/">标签</a>
      </li><li class="menu-item">
        <a class="menu-item-link" href="/service-computing/about/">关于</a>
      </li>
  </ul>
</nav>
    </header>

    <main id="main" class="main">
      <div class="content-wrapper">
        <div id="content" class="content">
          <article class="post">
    
    <header class="post-header">
      <h1 class="post-title"></h1>

      <div class="post-meta">
        <span class="post-time"> 0001-01-01 </span>
        
        
      </div>
    </header>

    <div class="post-toc" id="post-toc">
  <h2 class="post-toc-title"></h2>
  <div class="post-toc-content always-active">
    <nav id="TableOfContents">
  <ul>
    <li>
      <ul>
        <li><a href="#cli-命令行实用程序开发基础">CLI 命令行实用程序开发基础</a>
          <ul>
            <li><a href="#1概述">1、概述</a></li>
            <li><a href="#2基础知识">2、基础知识</a></li>
            <li><a href="#3golang-的支持">3、Golang 的支持</a></li>
            <li><a href="#4开发实践">4、开发实践</a></li>
            <li><a href="#5代码提交">5、代码提交</a></li>
          </ul>
        </li>
      </ul>
    </li>
  </ul>
</nav>
  </div>
</div>
    <div class="post-content">
      <h2 id="cli-命令行实用程序开发基础">CLI 命令行实用程序开发基础</h2>
<h3 id="1概述">1、概述</h3>
<p>  CLI（Command Line Interface）实用程序是Linux下应用开发的基础。正确的编写命令行程序让应用与操作系统融为一体，通过shell或script使得应用获得最大的灵活性与开发效率。Linux提供了cat、ls、copy等命令与操作系统交互；go语言提供一组实用程序完成从编码、编译、库管理、产品发布全过程支持；容器服务如docker、k8s提供了大量实用程序支撑云服务的开发、部署、监控、访问等管理任务；git、npm等都是大家比较熟悉的工具。尽管操作系统与应用系统服务可视化、图形化，但在开发领域，CLI在编程、调试、运维、管理中提供了图形化程序不可替代的灵活性与效率。</p>
<h3 id="2基础知识">2、基础知识</h3>
<p>  几乎所有语言都提供了完善的 CLI 实用程序支持工具。以下是一些入门文档（c 语言）：</p>
<ul>
<li><a href="https://www.ibm.com/developerworks/cn/linux/shell/clutil/index.html">开发 Linux 命令行实用程序</a>。</li>
<li><a href="https://wenku.baidu.com/view/c7cf91ee5ef7ba0d4a733b58.html">Linux命令行程序设计</a></li>
</ul>
<p>如果你熟悉 python ：</p>
<ul>
<li><a href="https://www.ibm.com/developerworks/aix/library/au-pythocli/index.html">Using Python to create UNIX command line tools</a></li>
</ul>
<p>阅读以后你应该知道 POSIX/GNU 命令行接口的一些概念与规范。命令行程序主要涉及内容：</p>
<ul>
<li>命令</li>
<li>命令行参数</li>
<li>选项：长格式、短格式</li>
<li>IO：stdin、stdout、stderr、管道、重定向</li>
<li>环境变量</li>
</ul>
<h3 id="3golang-的支持">3、Golang 的支持</h3>
<p>使用os，flag包，最简单处理参数的代码：</p>
<div class="highlight"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre class="chroma"><code><span class="lnt"> 1
</span><span class="lnt"> 2
</span><span class="lnt"> 3
</span><span class="lnt"> 4
</span><span class="lnt"> 5
</span><span class="lnt"> 6
</span><span class="lnt"> 7
</span><span class="lnt"> 8
</span><span class="lnt"> 9
</span><span class="lnt">10
</span><span class="lnt">11
</span><span class="lnt">12
</span><span class="lnt">13
</span></code></pre></td>
<td class="lntd">
<pre class="chroma"><code class="language-go" data-lang="go"><span class="kn">package</span> <span class="nx">main</span>

<span class="kn">import</span> <span class="p">(</span>
    <span class="s">&#34;fmt&#34;</span>
    <span class="s">&#34;os&#34;</span>
<span class="p">)</span>

<span class="kd">func</span> <span class="nf">main</span><span class="p">()</span> <span class="p">{</span>
    <span class="k">for</span> <span class="nx">i</span><span class="p">,</span> <span class="nx">a</span> <span class="o">:=</span> <span class="k">range</span> <span class="nx">os</span><span class="p">.</span><span class="nx">Args</span><span class="p">[</span><span class="mi">1</span><span class="p">:]</span> <span class="p">{</span>
        <span class="nx">fmt</span><span class="p">.</span><span class="nf">Printf</span><span class="p">(</span><span class="s">&#34;Argument %d is %s\n&#34;</span><span class="p">,</span> <span class="nx">i</span><span class="o">+</span><span class="mi">1</span><span class="p">,</span> <span class="nx">a</span><span class="p">)</span>
    <span class="p">}</span>

<span class="p">}</span>
</code></pre></td></tr></table>
</div>
</div><p>使用flag包的代码：</p>
<div class="highlight"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre class="chroma"><code><span class="lnt"> 1
</span><span class="lnt"> 2
</span><span class="lnt"> 3
</span><span class="lnt"> 4
</span><span class="lnt"> 5
</span><span class="lnt"> 6
</span><span class="lnt"> 7
</span><span class="lnt"> 8
</span><span class="lnt"> 9
</span><span class="lnt">10
</span><span class="lnt">11
</span><span class="lnt">12
</span><span class="lnt">13
</span><span class="lnt">14
</span><span class="lnt">15
</span></code></pre></td>
<td class="lntd">
<pre class="chroma"><code class="language-go" data-lang="go"><span class="kn">package</span> <span class="nx">main</span>

<span class="kn">import</span> <span class="p">(</span>
    <span class="s">&#34;flag&#34;</span> 
    <span class="s">&#34;fmt&#34;</span>
<span class="p">)</span>

<span class="kd">func</span> <span class="nf">main</span><span class="p">()</span> <span class="p">{</span>
    <span class="kd">var</span> <span class="nx">port</span> <span class="kt">int</span>
    <span class="nx">flag</span><span class="p">.</span><span class="nf">IntVar</span><span class="p">(</span><span class="o">&amp;</span><span class="nx">port</span><span class="p">,</span> <span class="s">&#34;p&#34;</span><span class="p">,</span> <span class="mi">8000</span><span class="p">,</span> <span class="s">&#34;specify port to use.  defaults to 8000.&#34;</span><span class="p">)</span>
    <span class="nx">flag</span><span class="p">.</span><span class="nf">Parse</span><span class="p">()</span>

    <span class="nx">fmt</span><span class="p">.</span><span class="nf">Printf</span><span class="p">(</span><span class="s">&#34;port = %d\n&#34;</span><span class="p">,</span> <span class="nx">port</span><span class="p">)</span>
    <span class="nx">fmt</span><span class="p">.</span><span class="nf">Printf</span><span class="p">(</span><span class="s">&#34;other args: %+v\n&#34;</span><span class="p">,</span> <span class="nx">flag</span><span class="p">.</span><span class="nf">Args</span><span class="p">())</span>
<span class="p">}</span>
</code></pre></td></tr></table>
</div>
</div><p>中文参考：</p>
<ul>
<li><a href="http://blog.studygolang.com/2013/02/%E6%A0%87%E5%87%86%E5%BA%93-%E5%91%BD%E4%BB%A4%E8%A1%8C%E5%8F%82%E6%95%B0%E8%A7%A3%E6%9E%90flag/">标准库—命令行参数解析FLAG</a></li>
<li><a href="https://studygolang.com/articles/5608">Go学习笔记：flag库的使用</a></li>
</ul>
<p>更多代码实践：</p>
<ul>
<li><a href="https://thenewstack.io/cli-command-line-programming-with-go/">cat demo</a></li>
<li><a href="https://github.com/golang/tools/blob/master/cmd/goimports/goimports.go">goimports 实现</a></li>
</ul>
<h3 id="4开发实践">4、开发实践</h3>
<p>使用 golang 开发 <a href="https://www.ibm.com/developerworks/cn/linux/shell/clutil/index.html">开发 Linux 命令行实用程序</a> 中的 <strong>selpg</strong></p>
<p>提示：</p>
<ol>
<li>请按文档 <strong>使用 selpg</strong> 章节要求测试你的程序</li>
<li>请使用 pflag 替代 goflag 以满足 Unix 命令行规范， 参考：<a href="https://o-my-chenjian.com/2017/09/20/Using-Flag-And-Pflag-With-Golang/">Golang之使用Flag和Pflag</a></li>
<li>golang 文件读写、读环境变量，请自己查 os 包</li>
<li>&ldquo;-dXXX&rdquo; 实现，请自己查 <code>os/exec</code> 库，例如案例 <a href="https://godoc.org/os/exec#example-Command">Command</a>，管理子进程的标准输入和输出通常使用  <code>io.Pipe</code>，具体案例见 <a href="https://godoc.org/io#Pipe">Pipe</a></li>
</ol>
<h3 id="5代码提交">5、代码提交</h3>
<ul>
<li>在 Github 提交程序，并在 readme.md 文件中描述设计说明，使用与测试结果。</li>
<li>如果你写了 关于 golang 或其他与课程相关的博客，请在课程群提交记录博客</li>
</ul>
<p>参考文献：<br>
[1] Package flag: <a href="https://go-zh.org/pkg/flag/">https://go-zh.org/pkg/flag/</a><br>
[2] Package os: <a href="https://go-zh.org/pkg/os/">https://go-zh.org/pkg/os/</a><br>
[3] <a href="https://thenewstack.io/cli-command-line-programming-with-go/">CLI: Command Line Programming with Go</a></p>

    </div>

    <div class="post-copyright">
  <p class="copyright-item">
    <span class="item-title"></span>
    <span class="item-content">潘茂林</span>
  </p>
  <p class="copyright-item">
    <span class="item-title"></span>
    <span class="item-content">
        0001-01-01
        
    </span>
  </p>
  
  
</div>
<footer class="post-footer">
      
      <nav class="post-nav">
        <a class="prev" href="/service-computing/post/ex-cli-agenda/">
            <i class="iconfont icon-left"></i>
            <span class="prev-text nav-default"></span>
            <span class="prev-text nav-mobile"></span>
          </a>
        <a class="next" href="/service-computing/post/ex-cloudgo-inout/">
            <span class="next-text nav-default"></span>
            <span class="next-text nav-mobile"></span>
            <i class="iconfont icon-right"></i>
          </a>
      </nav>
    </footer>
  </article>
        </div>
        

  

  

      </div>
    </main>

    <footer id="footer" class="footer">
      <div class="social-links">
      <a href="mailto:panml@mail.sysu.edu.cn" class="iconfont icon-email" title="email"></a>
  <a href="http://pmlpml.git.io/service-computing/index.xml" type="application/rss+xml" class="iconfont icon-rss" title="rss"></a>
</div>

<div class="copyright">
  <span class="power-by">
    %!(EXTRA string=<a class="hexo-link" href="https://gohugo.io">Hugo</a>)
  </span>
  <span class="division">|</span>
  <span class="theme-info">
     - 
    <a class="theme-link" href="https://github.com/olOwOlo/hugo-theme-even">Even</a>
  </span>

  

  <span class="copyright-year">
    &copy; 
    2018 - 
    2020
    <span class="heart">
      <i class="iconfont icon-heart"></i>
    </span>
    <span class="author">潘茂林</span>
  </span>
</div>
    </footer>

    <div class="back-to-top" id="back-to-top">
      <i class="iconfont icon-up"></i>
    </div>
  </div>
  
  <script src="https://cdn.jsdelivr.net/npm/jquery@3.2.1/dist/jquery.min.js" integrity="sha256-hwg4gsxgFZhOsEEamdOYGBf13FyQuiTwlAQgxVSNgt4=" crossorigin="anonymous"></script>
  <script src="https://cdn.jsdelivr.net/npm/slideout@1.0.1/dist/slideout.min.js" integrity="sha256-t+zJ/g8/KXIJMjSVQdnibt4dlaDxc9zXr/9oNPeWqdg=" crossorigin="anonymous"></script>
  <script src="https://cdn.jsdelivr.net/npm/@fancyapps/fancybox@3.1.20/dist/jquery.fancybox.min.js" integrity="sha256-XVLffZaxoWfGUEbdzuLi7pwaUJv1cecsQJQqGLe7axY=" crossorigin="anonymous"></script>



<script type="text/javascript" src="/service-computing/js/main.min.d7b7ada643c9c1a983026e177f141f7363b4640d619caf01d8831a6718cd44ea.js"></script>








</body>
</html>
